//
//  MTUserShildTableController.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/26.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTUserShildTableController.h"

@interface UserShieldCell ()

@property(nonatomic,strong) UIButton *headerButton;
@property(nonatomic,strong) UILabel *namelable;
@property(nonatomic,strong) UIButton *removeButton;
@end

@implementation UserShieldCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self makeUI];
    }
    return self;
}

-(void)makeUI
{
    _headerButton = [UIButton new];
    _namelable = [UILabel new];
    _removeButton = [UIButton new];
    [self.contentView addSubview:_headerButton];
    [self.contentView addSubview:_namelable];
    [self.contentView addSubview:_removeButton];
    
    _namelable.textColor = [UIColor blackColor];
    _namelable.font = [UIFont systemFontOfSize:14];

    [_removeButton setTitleColor:UIColorFromRGB(0x507DAF) forState:UIControlStateNormal];
    _removeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_removeButton addTarget:self action:@selector(removeClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_headerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(17);
        make.centerY.mas_equalTo(self.contentView.centerY).offset(0);
        make.width.mas_equalTo(@(40));
        make.height.mas_equalTo(@(40));
    }];
    
    [_namelable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerButton.mas_right).offset(10);
        make.centerY.mas_equalTo(self.contentView.centerY).offset(0);
    }];
    
    [_removeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
         make.centerY.mas_equalTo(self.contentView.centerY).offset(0);
    }];
}
-(void)setUser:(MTUserModel *)user
{
    _user = user;
    [_headerButton sd_setImageWithURL:[NSURL URLWithString:user.avatar] forState:UIControlStateNormal placeholderImage:defaultImgae];
    _namelable.text = _user.name;
    [_removeButton setTitle:@"移除屏蔽" forState:UIControlStateNormal];
    
}
-(void)removeClick
{
    if(self.removeBlockClick)
    {
        self.removeBlockClick(_user);
    }
}

@end



@interface MTUserShildTableController ()
@property(nonatomic,strong) NSMutableArray *users;
@end

@implementation MTUserShildTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"屏蔽用户";
    self.tableView.tableFooterView = [UIView new];
    self.tableView.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);
    self.tableView.rowHeight = 50;
    [self.tableView registerClass:[UserShieldCell class] forCellReuseIdentifier:@"UserShieldCellID"];
    [[MTWorksApiManager manager] getShieldUsersWithcomplation:^(NSMutableArray<MTUserModel *> *shieldUsers, MTError *error) {
       if(error == nil)
       {
           self.users = shieldUsers;
           [self.tableView reloadData];
       }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.users.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserShieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserShieldCellID"];
    cell.user = self.users[indexPath.row];
    @weakify(self);
    cell.removeBlockClick = ^(MTUserModel *user) {
        [MBProgressHUD showProcessHud:nil toView:nil];
        [[MTWorksApiManager manager] removeShieldUser:user.userId completion:^(MTError *boerror) {
            [MBProgressHUD hideHUDForView:nil];
            if(boerror == nil)
            {
                @strongify(self);
                [self.users removeObject:user];
                [self.tableView reloadData];
                [MBProgressHUD showSuccess:@"解除成功"];
            }
        }];
    };
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
